package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.SalSalary;
import com.cssl.pojo.SysStaff;
import com.cssl.service.SalSalaryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class SalSalaryController {
    @Resource
    private SalSalaryService service;

    @GetMapping("/cx")
    public IPage<SalSalary> select(Integer pageIndex, Integer pageSize){
        if (pageIndex ==null||pageIndex == 0){
            pageIndex =1;
        }
        if (pageSize == null || pageSize ==0){
            pageSize =5;
        }
        IPage<SalSalary> page=service.selet(new Page<SysStaff>(pageIndex, pageSize),new QueryWrapper<SysStaff>().eq("s.is_deleted",0));
        return page;
    }

    @GetMapping("/tjcx")
    public IPage<SalSalary> select2(Integer pageIndex, Integer pageSize,String name,String month,String code){
        if (pageIndex ==null||pageIndex == 0){
            pageIndex =1;
        }
        if (pageSize == null || pageSize ==0){
            pageSize =5;
        }
        IPage<SalSalary> page=service.selet(new Page<SysStaff>(pageIndex, pageSize),
                new QueryWrapper<SysStaff>().eq(name != ""&& name != null,"s2.name",name)
                        .eq(month != "" && month != null,"month",month)
                        .eq(code !="" && code != null,"s2.code",code)
                        .eq("s.is_deleted",0));
        return page;
    }

    @GetMapping("/tjcx2")
    public List<SalSalary> select2( String month, String code){
        if (month =="" || month==null){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            Date date = new Date();
            System.out.println(format.format(date));
            month=format.format(date);
        }
        List<SalSalary> page=service.select2(new QueryWrapper<SysStaff>().eq(month != "" && month != null,"month",month)
                        .eq(code !="" && code != null,"s2.code",code).eq("s.is_deleted",0));
        return page;
    }

    @GetMapping("/add")
    public boolean add(int id, BigDecimal jbgz, String month, BigDecimal jbf, BigDecimal shbt, BigDecimal zgz, BigDecimal ztkk, BigDecimal xjkk, BigDecimal kgkk, BigDecimal jj, String remark){
        SalSalary salSalary=new SalSalary(id,jbgz,jbf,shbt,jj,zgz,ztkk,xjkk,kgkk,month,remark);
        if (service.save(salSalary)){
            return true;
        }
        return false;
    }

    @GetMapping("/update")
    public boolean update(int id, BigDecimal jbgz, String month, BigDecimal jbf, BigDecimal shbt, BigDecimal zgz, BigDecimal ztkk, BigDecimal xjkk, BigDecimal kgkk, BigDecimal jj, String remark,int id2){
        SalSalary salSalary=new SalSalary();
        salSalary.setBase_salary(jbgz);
        salSalary.setId(id);
        salSalary.setBonus(jj);
        salSalary.setMonth(month);
        salSalary.setTotal_salary(zgz);
        salSalary.setOvertime_salary(jbf);
        salSalary.setRemark(remark);
        salSalary.setAbsenteeism_deduct(kgkk);
        salSalary.setLate_deduct(ztkk);
        salSalary.setLeave_deduct(xjkk);
        salSalary.setSubsidy(shbt);
        salSalary.setStaff_Id(id2);
        if (service.updateById(salSalary)){
            return true;
        }
        return false;
    }

    @GetMapping("/delete")
    public boolean delete(int id,int staffid){
        SalSalary salSalary=new SalSalary();
        salSalary.setId(id);
        salSalary.setIs_deleted(1);
        salSalary.setStaff_Id(staffid);
         if (service.updateById(salSalary)){
             return true;
         }
         return false;
    }

}
